﻿using System;
using System.Web;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Reflection;
using System.Collections.Generic;
using VIT.DataHelper.DataAccess;
using VIT.DataHelper.DataAccess;
using VIT.Wedding;

namespace DACS.Web.Modules.MenuCategory
{
    public class CategoryProvider
    {
        #region Properties
        public string LanguageId { get; set; }
        SQLSupport sqlSupport = new SQLSupport();
        #endregion

        #region Pre
        private DataTable GetList(int intParent)
        {
            StringBuilder strQuery = new StringBuilder();
            strQuery.Append("select Id, Title, ParentId ");
            strQuery.Append("FROM Category, Category_Language where Id=CategoryId and LanguageID=@LanguageID and ParentId=@Parent AND Category.Published = 1");
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            SqlParameter ParentId = new SqlParameter("@Parent", intParent);
            var result = sqlSupport.ExecuteDataset(false, strQuery.ToString(), LanguageId, ParentId).Tables[0];
            return result;
        }

        private int CountProductOfCat(int intCategoryId)
        {
            StringBuilder strQuery = new StringBuilder();
            strQuery.AppendFormat("SELECT COUNT(*) FROM Category INNER JOIN Products ON Category.Id=Products.CategoryId INNER JOIN Category_Language ON Category.Id=Category_Language.CategoryId WHERE Products.CategoryId={0} AND Category_Language.LanguageId='{1}' AND dbo.Products.Published = 1", intCategoryId, this.LanguageId);
            var result = sqlSupport.ExecuteScalar(false, strQuery.ToString());
            return Convert.ToInt32(result);
        }

        private void BuildMenuCat(MenuCat obj, int currentDepth, bool hasCounter = false)
        {
            if (obj == null) return;
            obj.Data = GetList(obj.Parent);
            if (obj.Data != null && obj.Data.Rows.Count > 0)
            {
                int count = obj.Data.Rows.Count;
                if (hasCounter)
                {
                    obj.Data.Columns.Add("Counter", typeof(int));
                }
                var lst = new List<MenuCat>();
                for (int i = 0; i < count; i++)
                {
                    if (hasCounter)
                    {
                        obj.Data.Rows[i]["Counter"] = CountProductOfCat(Convert.ToInt32(obj.Data.Rows[i]["Id"]));
                    }
                    if (currentDepth > 1)
                    {
                        var child = new MenuCat();
                        child.Parent = Convert.ToInt32(obj.Data.Rows[i]["Id"]);
                        BuildMenuCat(child, currentDepth - 1, hasCounter);
                        lst.Add(child);
                    }
                }
                obj.Children = lst.ToArray();
            }
        }

        public MenuCat GetMenuCategory(Dictionary<string, string> param)
        {
            int Parent;
            int.TryParse(param["Parent"], out Parent);
            bool HasCounter = false;
            bool.TryParse(param["HasCounter"], out HasCounter);
            int Depth = 1;
            int.TryParse(param["Depth"], out Depth);

            var result = new MenuCat();
            result.Parent = Parent;
            BuildMenuCat(result, Depth, HasCounter);
            return result;
        }

        public DataTable GetList(Dictionary<string, string> param)
        {
            int Parent;
            int.TryParse(param["Parent"], out Parent);

            var result = GetList(Parent);
            return result;
        }
        #endregion
    }

    public class MenuCat
    {
        public int Parent { get; set; }
        public DataTable Data { get; set; }
        public MenuCat[] Children { get; set; }
    }
}
